#include "iot_dido.h"
#include "iot_uart.h"

void InitPorts() {
    DigiInit(); // 初始化DI、DO端口
    IotUartAttribute uart_attr = {
        .baudRate = 115200,
        .dataBits = 8,
        .stopBits = 1,
        .parity = 0,
    };
    IoTUartInit(IOT_UART_IDX, &uart_attr); // 初始化UART端口
}

void ProcessData(const char* data) {
    JsonRoot root = JsonParse(data);
    if (root.protocolType == 0) { // GPIO
        // 处理DI/DO数据
        HandleGPIO(root.command);
    } else if (root.protocolType == 1) { // UART
        // 处理UART数据
        HandleUART(root.command);
    }
}

void HandleGPIO(const char* command) {
    // 根据command字符串处理DI/DO
}

void HandleUART(const char* command) {
    // 根据command字符串处理UART
    IoTUartWrite(IOT_UART_IDX, (unsigned char*)command, strlen(command));
}

void SendResponseToL2(const char* response) {
    IoTUartWrite(IOT_UART_IDX, (unsigned char*)response, strlen(response));
}

int main() {
    InitPorts();
    while (1) {
        char received_data[1024];
        // 假设从L2接收数据
        IoTUartRead(IOT_UART_IDX, received_data, sizeof(received_data));
        ProcessData(received_data);
        SendResponseToL2("Data processed");
        osDelay(1000); // 延迟一段时间，避免过快循环
    }
    return 0;
}